package com.hs.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.common.api.dto.PageBean;
import com.hs.common.api.dto.ResultCode;
import com.hs.common.api.dto.ResultVo;
import com.hs.order.config.OrderProducer;
import com.hs.order.dto.OrderDto;
import com.hs.order.pojo.Orders;
import com.hs.order.service.IOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author Bai
 * @since 2022-07-20
 */
@RestController
@RequestMapping("/orders")
@Slf4j
@Api(tags = "订单模块")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private OrderProducer orderProducer;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 条件查询(订单详情)
     * @param orderId
     * @param carLicensePlate
     * @param berthId
     * @return
     */
    @GetMapping("/condition")
    @ApiOperation("条件查询订单")
    public ResultVo findOrderByConditions(Long orderId,String carLicensePlate,Integer berthId,Integer status){

        return ordersService.findOrderByConditions(orderId,carLicensePlate,berthId,status);

    }

    /**
     * 通过车牌号查询车辆下所有订单信息
     * @param currentPage
     * @param pageSize
     * @param carNumber
     * @param status
     * @return
     */
    @GetMapping("/carLicensePlate")
    @ApiOperation("通过车牌号查询车辆下所有订单信息")
    public ResultVo findOrders(Integer currentPage,Integer pageSize,String carNumber,String status){

        IPage<Orders> page = new Page<>(currentPage,pageSize);

        QueryWrapper qr = new QueryWrapper<>();


        qr.eq("car_license_plate",carNumber);

        Integer integerStatus = null;
        if(!StringUtils.isEmpty(status)){
            if(status.equals("未支付")){ integerStatus = 0;}
            else if(status.equals("已支付")){ integerStatus = 1;}
            else if(status.equals("异常订单")){ integerStatus = 2;}
            else { integerStatus = 3;}

            qr.eq("status",integerStatus);
        }


        IPage orderPage = ordersService.page(page, qr);

        List records = orderPage.getRecords();
        Long total = orderPage.getTotal();

        if(!CollectionUtils.isEmpty(records)){
            ResultVo resultVo = new ResultVo(ResultCode.SUCCESS);
            PageBean<Orders> pageBean = new PageBean<>();
            pageBean.setList(records);
            pageBean.setTotals(total.intValue());

            resultVo.setData(pageBean);
            resultVo.setMsg("查询成功");

            return resultVo;
        }
        return new ResultVo(ResultCode.FAILED);
    }

    /**
     * 结束订单
     * @param orderId
     * @return
     */
    @GetMapping("/end/{orderId}")
    @ApiOperation("结束订单")
    public ResultVo finishOrder(@PathVariable("orderId") Long orderId){
        return ordersService.finishOrder(orderId);
    }


    /**
     * 查找指定用户下的未支付订单
     * @param tel
     * @return
     */
    @GetMapping("/unpay/{tel}")
    @ApiOperation("查询用户未支付订单")
    public ResultVo findUnPayOrders(@PathVariable("tel") String tel){
        return ordersService.findUnPayOrders(tel);
    }

    /**
     * 生产者发送信息（车辆为驶离）
     */
    @PostMapping("/send")
    @ApiOperation("第一次待确认订单")
    public void sendOrder(@RequestBody @Validated OrderDto orderDto){
        orderProducer.sendOrder(orderDto);
    }

    /**
     * 保存订单
     */
    @PostMapping("/save")
    @ApiOperation("生成订单")
    public ResultVo saveOrder(@RequestBody Orders orders){
        boolean save = ordersService.save(orders);
        return save ? new ResultVo() : new ResultVo(ResultCode.FAILED);
    }

    @GetMapping("/test")
    public void test(){
        Map<String,Object> dto = new HashMap<>();
        dto.put("泊位号",1);
        dto.put("时间",new Date());
        redisTemplate.opsForHash().putAll("tel",dto);
        System.out.println("success！");

        BoundHashOperations<String,String,Object> ops = redisTemplate.boundHashOps("tel");
        System.out.println("ops = " + ops);
        Integer o = (Integer) redisTemplate.opsForHash().get("tel", "泊位号");
        System.out.println("o = " + o);

    }



}

